﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WaterKit.DbBase;
using System.Data;
using WaterKit.Base;
using System.Collections;

namespace Auction_Server.Business
{
    public class baseDataService : OracleHelper
    {
        /// <summary>
        /// 根据类型获取数据字典数据
        /// </summary>
        /// <param name="dtype"></param>
        /// <returns>DataTable</returns>
        public DataTable getDictAsType(String dtype)
        {
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT * FROM DICT WHERE  DTYPE = '").Append(dtype).Append("'");
                return RetriveDataTable(sql.ToString());
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("根据类别获取数据字典数据出错", ex);
                return null;
            }
        }

        /// <summary>
        /// 根据类型获取数据字典数据
        /// </summary>
        /// <returns>DataTable</returns>
        public Hashtable getAllDict()
        {
            Hashtable HtDict = new Hashtable();
            try
            {
                DataTable dtDict = RetriveDataTable("SELECT * FROM DICT");
                for (int i = 0; i < dtDict.Rows.Count; i++)
                {
                    HtDict.Add(dtDict.Rows[i]["dcode"].ToString(), dtDict.Rows[i]["dname"].ToString());
                }
                return HtDict;
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("获取数据字典数据出错", ex);
                return null;
            }
        }

        /**
         * 保存竞价过程
         * target_no : 标的编号
         * price : 竞标价格
         * bid_company_no : 竞标单位编号
         * time : 出价时间
         **/
        public bool saveBidRecord(string target_no, double price, string bid_company_no, DateTime time)
        {
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("INSERT INTO BID_PRICE(TARGET_NO,PRICE,BIDUSER_NO,BIDTIME) VALUES(");
                sql.Append("'").Append(target_no).Append("'");
                sql.Append(",").Append(price);
                sql.Append(",'").Append(bid_company_no).Append("'");
                sql.Append(",to_date('").Append(time.ToString("yyyy-MM-dd hh:mm:ss")).Append("','YYYY-MM-DD HH24:MI:SS'))");

                return queryNonSQL(sql.ToString());
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("保存竞价过程出错", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取标的竞价记录
        /// </summary>
        /// <param name="target_no">标的编号</param>
        /// <returns>DataTable</returns>
        public DataTable getBid_Price(string target_no)
        {
            try
            {
                return RetriveDataTable("SELECT * FROM BID_PRICE WHERE TARGET_NO='" + target_no + "' ORDER BY BIDPRICE_ID DESC");
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("获取标的竞价记录出错", ex);
                return null;
            }
        }

        /// <summary>
        /// 更新商品状态
        /// </summary>
        /// <param name="product_no">商品编号</param>
        /// <param name="status">状态</param>
        /// <returns>布尔</returns>
        public bool setProductStatus(string product_no, string status)
        {
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("UPDATE PRODUCT SET PRODUCT_STATUS='");
                sql.Append(status).Append("'");
                sql.Append(" WHERE PRODUCT_NO='").Append(product_no).Append("'");

                return queryNonSQL(sql.ToString());
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("更新商品状态出错", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取成功标的竞拍人最高价确认书信息
        /// </summary>
        /// <param name="target_no">标的编号</param>
        /// <returns>DataTable</returns>
        public DataTable getTargetConfirmation(string target_no)
        {
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT * from BID_PRICE WHERE TARGET_NO='").Append(target_no).Append("' ORDER BY PRICE DESC");
                return RetriveDataTable(sql.ToString());
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("获取成功标的竞拍人最高价确认书信息出错", ex);
                return null;
            }
        }

        /// <summary>
        /// 用户登录操作
        /// </summary>
        /// <param name="loginName">登录名</param>
        /// <param name="loginPwd">登录密码</param>
        /// <param name="seatNum">座位号</param>
        /// <param name="targetNO">标的号</param>
        /// <returns>bool</returns>
        public bool UserLogin(string loginName, string loginPwd, string seatNum, string targetNO)
        {
            try
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("SELECT count(*) from AUCTION_USER a ,BIDUSER b where a.BIDUSER_NO IN (select BIDUSER_NO from BIDUSER where LOGIN_NAME='");
                sql.Append(loginName).Append("') AND a.AUCTION_USERPASSWORD='").Append(loginPwd).Append("' AND a.SEATNUMBER='").Append(seatNum).Append("' AND a.TARGET_NO='").Append(targetNO).Append("'");
                int rel = Convert.ToInt32(ExecuteScalar(sql.ToString()));
                return rel > 0 ? true : false;
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("用户登录操作", ex);
                return false;
            }
        }

        /// <summary>
        /// 获取终端列表
        /// </summary>
        public Hashtable SetTerminalMap()
        {
            Hashtable HtSeatIp = new Hashtable();
            try
            {
                string sql = string.Format("SELECT * FROM teminal");
                DataTable dt = RetriveDataTable(sql);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["IPADDRESS"] != null && HtSeatIp.ContainsKey(dt.Rows[i]["IPADDRESS"]) == false)
                    {
                        HtSeatIp.Add(dt.Rows[i]["IPADDRESS"].ToString(), dt.Rows[i]["SEATNUMBER"].ToString());
                    }
                }
                return HtSeatIp;
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("获取终端列表信息失败", ex);
                return null;
            }
        }

        /// <summary>
        /// 用户登录验证
        /// </summary>
        /// <param name="data">格式：{1}用户名|{2}密码</param>
        /// <returns></returns>
        public string userLogin(string data, string seatNum, string targetNo)
        {
            string retMsg = "";
            baseDataService user = new baseDataService();
            string[] noStr = data.Split('|');

            //解析用户名和密码
            if (noStr.Length != 2)
            {
                retMsg = "登录信息格式出错！";
            }

            //验证登录
            if (user.UserLogin(noStr[0], noStr[1], seatNum, targetNo) == false)
            {
                retMsg = "登录信息错误,请联系管理员核对！";
            }
            return retMsg;
        }

        /// <summary>
        /// 获取单位详细信息
        /// </summary>
        /// <param name="bidUser_no">单位编号</param>
        /// <returns></returns>
        public DataTable getUser(string bidUser_no)
        {
            try
            {
                string sql = string.Format("SELECT * FROM biduser WHERE BIDUSER_NO='" + bidUser_no + "'");
                return RetriveDataTable(sql);
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("获取单位详细信息失败", ex);
                return null;
            }
        }

        /// <summary>
        /// 获取用户证件列表
        /// </summary>
        /// <param name="bidUser_no"></param>
        /// <returns></returns>
        public DataTable getAttch(string bidUser_no)
        {
            try
            {
                string sql = string.Format("SELECT * FROM REC_INFO r,ATTACHMENT a WHERE r.BIDUSER_NO='" + bidUser_no + "' AND a.attachment_id=r.attachment_id");
                return RetriveDataTable(sql);
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("获取用户证件列表信息失败", ex);
                return null;
            }
        }
    }
}
